package com.jinglvhuanjing.app.base;


import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.alibaba.fastjson.JSON;
import com.jinglvhuanjing.app.bean.CityBean;
import com.jinglvhuanjing.app.listener.HttpListener;
import com.jinglvhuanjing.app.utils.Constants;
import com.jinglvhuanjing.app.utils.LOG;
import com.jinglvhuanjing.app.utils.SpUtils;
import com.jinglvhuanjing.app.widget.LeftDataView;

import java.lang.ref.WeakReference;

public abstract class CountDownTianQiActivity extends InstructionActivity{
    protected Handler mHandler;
    protected int time = 30 * 60;
    protected int mSecond = time;
    protected MyRunnable mRunnable;

    public abstract void countDownComplete();

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
    }

    protected void start(LeftDataView leftDataView) {
        if(this.mHandler == null){
            this.mHandler = new Handler(Looper.myLooper());
        }
        if(this.mRunnable == null){
            this.mRunnable = new MyRunnable(this);
        }
        this.mHandler.removeCallbacks(mRunnable);
        this.mHandler.post(this.mRunnable);

        String cityId = SpUtils.getString(getApplicationContext(), Constants.CITYID);
        requestGetTianqi(cityId, new HttpListener() {
            @Override
            public void onSuccess(String data) {
                LOG.e(TAG, "onSuccess:" + data);
                CityBean cityBean = JSON.parseObject(data, CityBean.class);
                SpUtils.putObject(getApplicationContext(), Constants.CITY_BEAN, cityBean);
                if (cityBean != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (leftDataView != null) {
                                leftDataView.setTianQi(cityBean);
                            } else {
                                LOG.e(TAG, "leftDataView null");
                            }
                        }
                    });
                }
            }

            @Override
            public void onFail(String fail) {
                LOG.e(TAG, "天气请求-onFail:" + fail);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mHandler.removeCallbacks(this.mRunnable);
        this.mRunnable = null;
    }

    private static class MyRunnable implements Runnable{
        private WeakReference<CountDownTianQiActivity> weakReference;
        private MyRunnable(CountDownTianQiActivity activity){
            weakReference = new WeakReference<>(activity);
        }
        @Override
        public void run() {
            CountDownTianQiActivity activity =  weakReference.get();
            if(activity == null){
                LOG.e(activity.TAG, "run null");
                return;
            }
            if(activity.mSecond <= 0){
                LOG.e(activity.TAG, "run countDownComplete");
                activity.mHandler.removeCallbacks(this);
                activity.countDownComplete();
                activity.mSecond = activity.time;
                return;
            }
            --activity.mSecond;
            activity.mHandler.postDelayed(this , 1000);
        }
    }
}
